﻿/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: Mouse event handler for annotation
 * Author: shaojie zhuo
 */

define(function (require, exports, module) {
    /**
     * @param baseAnnotPlugin
     * @constructor
     * @extends WebPDF.Event.IReaderMousePtHandler
     */
    var AnnotMousePtHandler = function (baseAnnotPlugin) {
        this.baseAnnotPlugin = baseAnnotPlugin;
    };
    AnnotMousePtHandler.prototype = {
        /**
         * @param event
         * @returns {{annot: WebPDF.PDFData.PDFAnnot, page: WebPDF.PDFView.PDFPageView}}
         * @private
         */
        _getPageAndAnnot: function(target){         
            var annot = null, pageView = null;
            var annotDiv = null;
           if($(target).hasClass("fwr-annot")){
              annotDiv = target;
           }else {
              var parentDiv = $(target).parents(".fwr-annot");
              if (parentDiv[0] != undefined){
                    annotDiv = parentDiv[0];
               }
            }
            
            if(annotDiv){
                var pageIndex = parseInt(annotDiv.getAttribute("page-index"));
                var annotName = annotDiv.getAttribute("annot-name");
                var docView = this.baseAnnotPlugin.getReaderApp().getMainView().getDocView();
                pageView = docView.getPageView(pageIndex);
                var pdfPage = pageView.getPDFPage();
                annot = pdfPage.getAnnotByName(annotName);
            }
            return {annot: annot, page: pageView};
        },
        getType : function(){
            return "Annot Mouse Pt Handler";
        },
        onLButtonDown: function(event){
            var pageAndAnnot = this._getPageAndAnnot(event.target);
            return this.baseAnnotPlugin.onAnnotLButtonDown(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onLButtonUp: function(event){            
            var pageAndAnnot = this._getPageAndAnnot(event.target); 
            return this.baseAnnotPlugin.onAnnotLButtonUp(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onLButtonDblClk: function(event){
            var pageAndAnnot = this._getPageAndAnnot(event.target);
            return this.baseAnnotPlugin.onAnnotLButtonDblClk(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onMouseMove: function(event){          
            var pageAndAnnot = this._getPageAndAnnot(event.target);
            return this.baseAnnotPlugin.onAnnotMouseMove(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onRButtonDown: function(event){
            return false;
        },
        onRButtonUp: function(event){
            return false;
        },
        onRButtonDblClk: function(event){
            return false;
        },
        onMouseWheel: function(event){
            return false;
        },
        onMouseOver: function(event){
            var pageAndAnnot = this._getPageAndAnnot(event.target);
            return this.baseAnnotPlugin.onAnnotMouseOver(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onMouseOut: function(event){
            return this.onMouseLeave(event);
        },
        onMouseLeave: function(event){
            var pageAndAnnot = this._getPageAndAnnot(event.fromElement);
            return this.baseAnnotPlugin.onAnnotMouseLeave(pageAndAnnot.page, pageAndAnnot.annot, event);
        },
        onMouseEnter: function(event){
            return this.onMouseOver(event);
        },
        onDoubleTap: function(event) {
            return false;
        },
        onPinchIn: function(event) {
            return false;
        },
        onPinchOut: function(event) {
            return false;
        },
        onHold: function(event) {
            return false;
        }
    };
    WebPDF.Event.AnnotMousePtHandler = AnnotMousePtHandler;
    return WebPDF.Event.AnnotMousePtHandler;
});